//
//  XGExamCell.m
//  XGTableView的用法详解
//
//  Created by 小果 on 16/7/5.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "XGExamCell.h"
#import "XGExamModel.h"
// 根据文字字体的大小和内容计算其真实尺寸
#import "NSString+Extension.h"
#import "XGExamFrame.h"


#define XGBottomSubViewsColor [UIColor lightGrayColor]
#define XGBottomTitleColor [UIColor blueColor]

@interface XGExamCell()
/**
 *  头像
 */
@property (nonatomic, weak) UIImageView *iconView;
/**
 *  标题
 */
@property (nonatomic, weak) UILabel *nameLabel;
/**
 *  正文
 */
@property (nonatomic, weak) UILabel *contentLabel;
/**
 *  会员
 */
@property (nonatomic, weak) UIImageView *vipView;

/**
 *  底部的工具条
 */
@property (nonatomic, weak) UIView *bottomView;
/**
 *  底部工具条上面的分割线
 */
//@property (nonatomic, weak) UIView *bottomTopView;
/**
 *  cell的分割线
 */
@property (nonatomic, weak) UIView *devier;
@property (nonatomic,strong)CADisplayLink *link;
@end

@implementation XGExamCell
#pragma mark - 对cell的重用进行封装
+(instancetype)cellWithTableView:(UITableView *)tableView
{
    // 定义一个可循环引用的标识
    static NSString *ID = @"exam";
    
    // 从缓存池中找这个标识
    XGExamCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 如果没有找到，就重新实例化一个cell
    if(nil == cell){
        cell = [[XGExamCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}

#pragma mark - 重写cell的initWithStye:reuseIdentifier
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    // 判断
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        
        // 重新实例化cell中的每个控件
        [self setUpSubViews];
    }
    
    return self;
}

#pragma mark - 实例化cell中的每个控件
- (void)setUpSubViews{
    // 头像
    UIImageView *iconImage = [[UIImageView alloc] init];
    // 给图片添加一个手势
    iconImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *handClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconImageClick)];
    [iconImage addGestureRecognizer:handClick];
    // 设置边框的颜色
    iconImage.layer.borderColor = [UIColor greenColor].CGColor;
    // 设置边框的宽度
    iconImage.layer.borderWidth = 2;
    [self.contentView addSubview:iconImage];
    self.iconView = iconImage;
    
    // 标题
    UILabel *nameLabel = [[UILabel alloc] init];
    // 设置标题文字的尺寸
    nameLabel.font = XGNameFont;
    nameLabel.numberOfLines = 0;
    [self.contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    // 会员
    UIImageView *vipImage = [[UIImageView alloc] init];
    // 设置图片
    vipImage.image = [UIImage imageNamed:@"vip"];
    
    [self.contentView addSubview:vipImage];
    self.vipView = vipImage;
    
    // 正文
    UILabel *content = [[UILabel alloc] init];
    content.textColor = XGRandomColor;
    // 设置是否换行
    content.numberOfLines = 0;
    // 设置正文文字的尺寸
    content.font = XGContentFont;
    [self.contentView addSubview:content];
    self.contentLabel = content;
    
    // 配图
    UIButton *picture = [[UIButton alloc] init];
    // 设置颜色
    picture.backgroundColor = XGRandomColor;
    picture.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    [picture addTarget:self action:@selector(pictureClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:picture];
    self.pictureView = picture;
    
    // 底部的View
    UIView *bottomView = [[UIView alloc] init];
    self.bottomView = bottomView;
    [self.contentView addSubview:bottomView];
    
    // 转发按钮
    UIButton *relayBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 6, 50, 20)];
    relayBtn.layer.cornerRadius = 10;
    relayBtn.layer.masksToBounds = YES;
    relayBtn.titleLabel.font = XGNameFont;
    [relayBtn setTitle:@"转发" forState:UIControlStateNormal];
    [relayBtn setBackgroundColor:XGBottomSubViewsColor];
    [relayBtn setTitleColor:XGBottomTitleColor forState:UIControlStateNormal];
    [self.bottomView addSubview:relayBtn];
    
    // 评论按钮
    UIButton *commentBtn  = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 10 - relayBtn.frame.size.width, relayBtn.frame.origin.y, relayBtn.frame.size.width, relayBtn.frame.size.height)];
    commentBtn.layer.cornerRadius = 10;
    commentBtn.layer.masksToBounds = YES;
    commentBtn.titleLabel.font = XGNameFont;
    [commentBtn setTitle:@"评论" forState:UIControlStateNormal];
    [commentBtn setBackgroundColor:XGBottomSubViewsColor];
    [commentBtn setTitleColor:XGBottomTitleColor forState:UIControlStateNormal];
    [self.bottomView addSubview:commentBtn];
    
    // 点赞按钮
    UIButton *supportBtn = [[UIButton alloc] initWithFrame:CGRectMake(commentBtn.frame.origin.x - 10 - relayBtn.frame.size.width, relayBtn.frame.origin.y, relayBtn.frame.size.width, relayBtn.frame.size.height)];
    supportBtn.layer.cornerRadius = 10;
    supportBtn.layer.masksToBounds = YES;
    supportBtn.titleLabel.font = XGNameFont;
    [supportBtn setTitle:@"赞" forState:UIControlStateNormal];
    [supportBtn setBackgroundColor:XGBottomSubViewsColor];
    [supportBtn setTitleColor:XGBottomTitleColor forState:UIControlStateNormal];
    [self.bottomView addSubview:supportBtn];
    
    // 底部工具条上面的分割线
    //    UIView *bottomTopView = [[UIView alloc] init];
    //    self.bottomTopView = bottomTopView;
    //    bottomTopView.backgroundColor = XGRandomColor;
    //    [self.bottomView addSubview:bottomTopView];
    
    // cell的分割线
    UIView *devier = [[UIView alloc] init];
    self.devier = devier;
    devier.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:devier];
}

#pragma mark - 用frame模型的做法
-(void)setExamFrame:(XGExamFrame *)examFrame
{
    _examFrame = examFrame;
    
    // 设置数据
    [self settingSubViewsWithData:examFrame];
    
    // 设置frame
    [self settingSubViewsWithFrame:examFrame];
}

#pragma mark - 给每个对象赋值
-(void)settingSubViewsWithData:(XGExamFrame *)examFrame{
    
    XGExamModel *examModel = self.examFrame.examM;
    // 头像
    self.iconView.image = [UIImage imageNamed:examModel.icon];
    
    // 会员
    if(examModel.isVip){
        // 如果是VIP，就让它显示，并设置标题的颜色为红色
        self.vipView.hidden = NO;
        //  标题颜色
        self.nameLabel.textColor = [UIColor redColor];
    }else{ // 如果不是VIP,就让它隐藏，并设置标题的颜色为黑色
        self.vipView.hidden = YES;
        // 标题的颜色
        self.nameLabel.textColor = [UIColor blackColor];
        
    }
    
    // 标题
    self.nameLabel.text = examModel.name;
    
    // 正文
    self.contentLabel.text = examModel.content;
    
    // 配图
    if(examModel.picture){// 如果有配图就让其显示
        self.pictureView.hidden = NO;
        
        [self.pictureView setImage:[UIImage imageNamed:examModel.picture] forState:UIControlStateNormal];
        
    }else{// 如果没有配图就隐藏
        self.pictureView.hidden = YES;
    }
    
    
}

#pragma mark - 计算每个控件的frame
-(void)settingSubViewsWithFrame:(XGExamFrame *)examFrame{
    //头像的frame
    self.iconView.frame = examFrame.iconF;
    // 标题的frame
    self.nameLabel.frame = examFrame.nameF;
    // 会员的frame
    self.vipView.frame = examFrame.vipF;
    // 正文的frame
    self.contentLabel.frame = examFrame.contentF;
    // 配图的frame
    if (self.examFrame.examM.picture){
        self.pictureView.frame = examFrame.pictureF;
    }
    // 底部工具条的frame
    self.bottomView.frame = examFrame.bottomF;
    
    // 底部工具条上面的frame
    //    self.bottomTopView.frame = examFrame.bottomTopF;
    
    // cell的分割线
    self.devier.frame = examFrame.devierF;
    
}

#pragma mark - 监听头像的点击
-(void)iconImageClick{
    if ([self.delegate respondsToSelector:@selector(examCellWithIconImageAddHandclick:)]){
        [self.delegate examCellWithIconImageAddHandclick:self];
    }
    
    // 用通知的方法来实现头像的点击
    //    [[NSNotificationCenter defaultCenter] postNotificationName:@"iconImageClick" object:self userInfo:@{@"icon":_examFrame.examM.icon}];
    
}

#pragma mark - 监听配图的点击事件
-(void)pictureClick{
    // 发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pictureClick" object:self userInfo:@{@"picture":_examFrame.examM.picture}];
}

#pragma mark - 重新布局头像的尺寸(为头像设置圆角)
-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.iconView.layer.cornerRadius = self.iconView.frame.size.width * 0.5;
    self.iconView.layer.masksToBounds = YES;
    
}


#pragma mark - 不用frame模型的做法
/**
 #pragma mark - 在这个方法里面设置子控件的frame和数据（因为自定义的cell里面的子控件的frame是有数据模型来决定的;对cell中的每个控件的属性赋值，并设置其frame
 -(void)setExamModel:(XGExamModel *)examModel{
 _examModel = examModel;
 
 // 设置数据
 [self settingSubViewsWithData:examModel];
 
 // 设置frame
 [self settingSubViewsWithFrame:examModel];
 
 }
 
 #pragma mark - 给每个控件设置数据
 -(void)settingSubViewsWithData:(XGExamModel *)examModel{
 
 // 头像
 self.iconView.image = [UIImage imageNamed:examModel.icon];
 
 // 会员
 if(examModel.isVip){
 // 如果是VIP，就让它显示，并设置标题的颜色为红色
 self.vipView.hidden = NO;
 //  标题颜色
 self.nameLabel.textColor = [UIColor redColor];
 }else{ // 如果不是VIP,就让它隐藏，并设置标题的颜色为黑色
 self.vipView.hidden = YES;
 // 标题的颜色
 self.nameLabel.textColor = [UIColor blackColor];
 
 }
 
 // 标题
 self.nameLabel.text = examModel.name;
 
 // 正文
 self.contentLabel.text = examModel.content;
 
 // 配图
 if(examModel.picture){// 如果有配图就让其显示
 self.pictureView.hidden = NO;
 
 [self.pictureView setImage:[UIImage imageNamed:examModel.picture] forState:UIControlStateNormal];
 
 }else{// 如果没有配图就隐藏
 self.pictureView.hidden = YES;
 }
 
 }
 
 #pragma mark - 给每个控件设置frame(不用frame模型的做法)
 -(void)settingSubViewsWithFrame:(XGExamModel *)examModel{
 
 // 每个控件之间的间距
 CGFloat padding = 10;
 
 // 头像的frame
 CGFloat iconX = padding;
 CGFloat iconY = padding;
 CGFloat iconW = 30;
 CGFloat iconH = 30;
 self.iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
 
 // 标题的frame
 // 先计算出文字的真实尺寸
 CGSize nameSize = [self.examModel.name sizeWithFont:XGNameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) ];
 CGFloat nameX = CGRectGetMaxX(self.iconView.frame) + padding;
 CGFloat nameY = iconY + (iconH - nameSize.height) * 0.5;
 self.nameLabel.frame = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
 
 
 // 会员的frame
 CGFloat vipX = CGRectGetMaxX(self.nameLabel.frame) + padding;
 CGFloat vipW = 14;
 CGFloat vipH = vipW;
 CGFloat vipY = iconY + (iconH - vipH)*0.5;
 self.vipView.frame = CGRectMake(vipX, vipY, vipW, vipH);
 
 
 // 正文的frame
 CGFloat contentX = padding;
 CGFloat contentY = CGRectGetMaxY(self.iconView.frame) + padding;
 CGFloat contentW = [UIScreen mainScreen].bounds.size.width - 2 * padding;
 CGSize contentSize = [self.contentLabel.text sizeWithFont:XGContentFont maxSize:CGSizeMake(contentW, MAXFLOAT)];
 self.contentLabel.frame = CGRectMake(contentX, contentY, contentSize.width, contentSize.height);
 
 // 配图的frame
 if(examModel.picture){// 如果有配图就计算其frame，并计算出每行的高度
 CGFloat pictureW = 200;
 CGFloat pictureH = 150;
 CGFloat pictureX = ([UIScreen mainScreen].bounds.size.width - pictureW) * 0.5;
 CGFloat pictureY = CGRectGetMaxY(self.contentLabel.frame) + padding;
 self.pictureView.frame = CGRectMake(pictureX, pictureY, pictureW, pictureH);
 
 examModel.cellHeight = CGRectGetMaxY(self.pictureView.frame) + padding;
 }else{
 // 如果没有配图，直接计算每行的高度即可
 examModel.cellHeight = CGRectGetMaxY(self.contentLabel.frame) + padding;
 }
 
 }
 */
@end
